﻿int fps = 100;
long currentTime, lastTime;
int w = 60, h = 30;// 设置屏幕大小
int xuanxiang = 0;
ConsoleKeyInfo key;
ConsoleKeyInfo move;
Selection sel;
Direction dir;
Loc loc;
int nowScence = 0;//设置场景编号
#region 输入
void Input()
{
    if (nowScence == 0)//是否转换场景
    {
        key = Console.ReadKey(true);
        //开始界面选项控制
        switch (key.Key)
        {
            case ConsoleKey.UpArrow:
            case ConsoleKey.W:
                sel = Selection.Up;
                break;
            case ConsoleKey.DownArrow:
            case ConsoleKey.S:
                sel = Selection.down;
                break;
            case ConsoleKey.J:
                sel = Selection.jj;
                break;
        }
    }
    else if (nowScence == 1)//是否转换场景
    {
        move = Console.ReadKey(true);  // 参数控制是否显示字符
        //移动
        switch (move.Key)
        {
            case ConsoleKey.UpArrow:
            case ConsoleKey.W:
                dir = Direction.Up;
                break;
            case ConsoleKey.DownArrow:
            case ConsoleKey.S:
                dir = Direction.Down;
                break;
            case ConsoleKey.LeftArrow:
            case ConsoleKey.A:
                dir = Direction.Left;
                break;
            case ConsoleKey.RightArrow:
            case ConsoleKey.D:
                dir = Direction.Right;
                break;
            default:
                break;

        }
    }
}
#endregion

#region 初始化
void Init()
{
    //隐藏光标
    Console.CursorVisible = false;
    //设置标题
    Console.Title = "推箱子";
    //设置控制台变量
    int w = 60, h = 30;
    //设置控制台大小
    Console.SetWindowSize(w, h);
    Console.SetBufferSize(w + 20, h + 20);
    //开始界面选项
    sel = Selection.None;
    //出生点坐标
    loc.x = w / 2;
    loc.y = h / 2;
    //移动
    dir = Direction.None;
}

#endregion

#region 更新
void Update()
{
    Input();
    if (nowScence == 0)//是否转换场景
    {
        //开始界面选项变化
        switch (sel)
        {
            case Selection.Up:
                --xuanxiang;
                if (xuanxiang < 0)
                {
                    xuanxiang = 0;
                }
                break;
            case Selection.down:
                ++xuanxiang;
                if (xuanxiang > 1)
                {
                    xuanxiang = 1;
                }
                break;
            case Selection.jj:
                if (xuanxiang == 0)
                {
                    nowScence = 1;
                }
                else
                {
                    Environment.Exit(0);
                }
                break;
            default:
                break;
        }
    }
    else if (nowScence == 1)//是否转换场景
    {
        switch (dir)
        {
            case Direction.Up:
                loc.y--;
                break;
            case Direction.Down:
                loc.y++;
                break;
            case Direction.Left:
                loc.x--;
                break;
            case Direction.Right:
                loc.x++;
                break;
        }
        // 限定范围
        if (loc.x < 2) loc.x = 2;
        if (loc.x > w - 4) loc.x = w - 4;
        if (loc.y < 1) loc.y = 1;
        if (loc.y > h - 2) loc.y = h - 2;
    }
}

#endregion

#region 渲染
void Render()
{
    if (nowScence == 0)//是否转换场景
    {
        
        //设置标题以及位置
        Console.ResetColor();
        Console.SetCursorPosition(w / 2 - 3, h / 4);
        Console.WriteLine("推箱子");
        //设置开始选项
        Console.ForegroundColor = xuanxiang == 0 ? ConsoleColor.Red : ConsoleColor.White;
        Console.SetCursorPosition(w / 2 - 4, h / 4 + 4);
        Console.WriteLine("开始游戏");
        Console.ForegroundColor = xuanxiang == 1 ? ConsoleColor.Red : ConsoleColor.White;
        Console.SetCursorPosition(w / 2 - 4, h / 4 + 6);
        Console.WriteLine("退出游戏");
    }
    else if (nowScence == 1)//是否转换场景
    {
        //Console.Clear();
        // 画面
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.SetCursorPosition(loc.x , loc.y);//设置方块坐标
        Console.Write("■");
        //设置围墙
        Console.ForegroundColor = ConsoleColor.DarkGray;
        for (int i = 0; i < w; i++)
        {
            for (int j = 0; j < h; j++)
            {
                if (i == 0 || j == 0 || i == w - 2 || j == h - 1)
                {
                    Console.SetCursorPosition(i, j);
                    Console.Write("■");
                }
            }
        }
    }
}

#endregion

#region 结束
void End()
{

}

#endregion

#region 游戏
void Game()
{
    Init();
    lastTime = DateTime.Now.Ticks;
    while (true)
    {
        Update();
        currentTime = DateTime.Now.Ticks;
        if (currentTime - lastTime > 10_000_000 / fps)
        {
            Render();
            lastTime = DateTime.Now.Ticks;
        }
    }
    End();
}

#endregion
Game();
enum Selection//开始界面选项
{
    None,
    Up,
    down,
    jj,
}
enum Direction//移动
{
    None,
    Up,
    Down,
    Left,
    Right
}
struct Loc//出生点
{
    public int x;
    public int y;
}